package com.blockchain.console.servlets.content.zxgl;

import java.sql.Timestamp;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.ArticleType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.console.service.content.ArticleManage;
import com.blockchain.console.service.content.entity.Article;
import com.blockchain.console.service.content.entity.ArticleRecord;
import com.blockchain.console.servlets.content.AbstractContentServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@MultipartConfig
@Right(id = "C_CONTENT_ZX_UPDATE", name = "资讯管理-修改")
public class UpdateZx extends AbstractContentServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(final HttpServletRequest request, HttpServletResponse response,
			ServiceSession serviceSession) throws Throwable {
		ArticleType type = EnumParser.parse(ArticleType.class, request.getParameter("type"));
		try {
			Article article = new Article() {

				@Override
				public String getTitle() {
					return request.getParameter("title");
				}

				@Override
				public String getSummary() {
					return request.getParameter("summary");
				}

				@Override
				public String getSource() {
					return request.getParameter("source");
				}

				@Override
				public int getSortIndex() {
					return IntegerParser.parse(request.getParameter("sortIndex"));
				}

				@Override
				public IsPass getPublishStatus() {
					return EnumParser.parse(IsPass.class, request.getParameter("status"));
				}

				@Override
				public UploadFile getImage() throws Throwable {
					return null;
				}

				@Override
				public String getContent() {
					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("content"));
				}

				@Override
				public Timestamp publishTime() {
					return TimestampParser.parse(request.getParameter("publishTime"));
				}

				@Override
				public IsPass getIstj() {
					return EnumParser.parse(IsPass.class, request.getParameter("istj"));
				}

				@Override
				public int getIsorder() {
					return IntegerParser.parse(request.getParameter("isorder"));
				}

				@Override
				public String getTitleEn() {
					return request.getParameter("titleEn");
				}

				@Override
				public String getTitleTw() {
//					return request.getParameter("titleTw");
					return "test";
				}

				@Override
				public String getContentEn() {
					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("contentEn"));
				}

				@Override
				public String getContentTw() {
//					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("contentTw"));
					return "test";
				}
			};
			ArticleManage manage = serviceSession.getService(ArticleManage.class);
			int zxid = IntegerParser.parse(request.getParameter("id"));
			manage.update(zxid, article, type);
			sendRedirect(request, response, getController().getURI(request, SearchZx.class));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}

	@Override
	protected void processGet(final HttpServletRequest request, HttpServletResponse response,
			ServiceSession serviceSession) throws Throwable {
		ArticleManage manage = serviceSession.getService(ArticleManage.class);
		ArticleRecord record = manage.get(IntegerParser.parse(request.getParameter("id")));
		if (record == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		String content = manage.getContent(IntegerParser.parse(request.getParameter("id")));
		String contentEn = manage.getContentEn(IntegerParser.parse(request.getParameter("id")));
		String contentTw = manage.getContentTw(IntegerParser.parse(request.getParameter("id")));
		request.setAttribute("content", content);
		request.setAttribute("contentEn", contentEn);
		request.setAttribute("contentTw", contentTw);
		request.setAttribute("record", record);
		forwardView(request, response, getClass());
	}

}
